
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>函数句柄 - fubaisen</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="函数句柄函数句柄其实是MATLAB中一种间接调用函数的方法。在MATLAB平台中，对函数的调用主要有直接调用和间接调用两种方式。直接调用顾名思义就是直接使用某个函数，但是使用该函数只能与其M文件同名,"> 
    <meta name="author" content="John Doe"> 
    <link rel="alternative" href="atom.xml" title="fubaisen" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    
    
    
    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:title" content="函数句柄 - fubaisen"/>
    <meta name="twitter:description" content="函数句柄函数句柄其实是MATLAB中一种间接调用函数的方法。在MATLAB平台中，对函数的调用主要有直接调用和间接调用两种方式。直接调用顾名思义就是直接使用某个函数，但是使用该函数只能与其M文件同名,"/>
    
    
    
    
    <meta property="og:site_name" content="fubaisen"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="函数句柄 - fubaisen"/>
    <meta property="og:description" content="函数句柄函数句柄其实是MATLAB中一种间接调用函数的方法。在MATLAB平台中，对函数的调用主要有直接调用和间接调用两种方式。直接调用顾名思义就是直接使用某个函数，但是使用该函数只能与其M文件同名,"/>
    
<link rel="stylesheet" href="/css/diaspora.css">

    <script>window.searchDbPath = "/search.xml";</script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<meta name="generator" content="Hexo 6.3.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">fubaisen</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="http://fubaisen.github.io"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">函数句柄</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">函数句柄</h1>
        <div class="stuff">
            <span>三月 31, 2022</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/MATLAB%E5%AD%A6%E4%B9%A0/" rel="tag">MATLAB学习</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="函数句柄"><a href="#函数句柄" class="headerlink" title="函数句柄"></a>函数句柄</h1><p>函数句柄其实是<code>MATLAB</code>中一种间接调用函数的方法。在<code>MATLAB</code>平台中，对函数的调用主要有直接调用和间接调用两种方式。直接调用顾名思义就是直接使用某个函数，但是使用该函数只能与其M文件同名的主函数或在M文件中的其他函数所调用，同时在一个文件中也只能存在一个主函数。函数句柄的出现就是为了解决这个问题。函数句柄所用到的操作符为<code>@</code>，使用起来是十分的方便。 函数句柄的创建和调用：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt; F_Handle = @<span class="built_in">cos</span>           <span class="comment">% cos 以弧度为单位的参数的余弦</span></span><br><span class="line">F_Handle =</span><br><span class="line">  包含以下值的 function_handle:</span><br><span class="line">    @<span class="built_in">cos</span></span><br><span class="line">&gt;&gt; X = <span class="number">0</span>:<span class="number">0.25</span>*<span class="built_in">pi</span>:<span class="number">2</span>*<span class="built_in">pi</span>;</span><br><span class="line">&gt;&gt; F_Handle(X)</span><br><span class="line"><span class="built_in">ans</span> =</span><br><span class="line">  <span class="number">1</span> 至 <span class="number">6</span> 列</span><br><span class="line">    <span class="number">1.0000</span>    <span class="number">0.7071</span>    <span class="number">0.0000</span>   <span class="number">-0.7071</span>   <span class="number">-1.0000</span>   <span class="number">-0.7071</span></span><br><span class="line">  <span class="number">7</span> 至 <span class="number">9</span> 列</span><br><span class="line">   <span class="number">-0.0000</span>    <span class="number">0.7071</span>    <span class="number">1.0000</span></span><br></pre></td></tr></table></figure>

<p>函数句柄的基本操作：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt; F_Handlea = @<span class="built_in">exp</span>;</span><br><span class="line">&gt;&gt; F_Handleb = @<span class="built_in">log</span>;</span><br><span class="line">&gt;&gt; functions(F_Handlea)</span><br><span class="line"><span class="built_in">ans</span> = </span><br><span class="line">  包含以下字段的 struct:</span><br><span class="line">    <span class="function"><span class="keyword">function</span>: &#x27;<span class="title">exp</span>&#x27;</span></span><br><span class="line">        <span class="built_in">type</span>: <span class="string">&#x27;simple&#x27;</span></span><br><span class="line">        file: <span class="string">&#x27;&#x27;</span></span><br><span class="line">&gt;&gt; isa(F_Handlea,<span class="string">&#x27;function_handle&#x27;</span>)  <span class="comment">%判断是否属于函数句柄</span></span><br><span class="line"><span class="built_in">ans</span> =</span><br><span class="line">  logical</span><br><span class="line">   <span class="number">1</span></span><br><span class="line">&gt;&gt; <span class="built_in">isequal</span>(F_Handlea,F_Handleb)      <span class="comment">%判断两函数句柄是否相等</span></span><br><span class="line"><span class="built_in">ans</span> =</span><br><span class="line">  logical</span><br><span class="line">   <span class="number">0</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>function用来定义函数，一般一个函数放在一个.m文件里</p>
<p>举个简单的例子建立一个myfunction.m，然后在文件中写。<br><code>function y=myfunction(a,b)</code></p>
<ul>
<li><p>其中a,b是输入函数的参数</p>
</li>
<li><p>y是函数返回的值</p>
</li>
</ul>
<p>当需要返回多个值时，可以将 <code>y</code> 看作一个数组，或者直接将函数的开头写成如<code>function [x,y]=myfunction(x,y)</code>的形式。比如最简单的<code>y=a+b</code></p>
<p>总结一下，将下面的代码<br><code>function y=myfunction(a,b)</code><br><code>y=a+b;</code><br>保存为一个m文件myfunction.m，调用方式如下<br><code>num=myfunction(3,4);</code><br>这样就可以由函数中的y&#x3D;a+b得到<code>num的值是3+4=7。</code></p>
<p>在matlab中调用函数只能在.m文件中定义，并且文件名和函数名形同</p>
<p>例子：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">[q,w,e]=test(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">% matlab用function定义函数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="params">[a,b,c]</span>=<span class="title">test</span><span class="params">(d,e,f)</span></span></span><br><span class="line"></span><br><span class="line">a=d+e;</span><br><span class="line"></span><br><span class="line">b=e+f;</span><br><span class="line"></span><br><span class="line">c=f+d;</span><br><span class="line"></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<p><img src="https://s2.loli.net/2022/03/31/TPzucidJLB8YASf.png" alt="image-20220331002141717"></p>
</blockquote>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
        
    <div id="gitalk-container" class="comment link"
		data-enable="false"
        data-ae="false"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
</body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/typed.js"></script>
<script src="/js/diaspora.js"></script>


<link rel="stylesheet" href="/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/photoswipe/default-skin/default-skin.css">


<script src="/photoswipe/photoswipe.min.js"></script>
<script src="/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
